from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class MapTile(QWidget):
    attrs = ["x", "y", "tile_id", "frames"]
    def __init__(self, x, y, sprite, tile_id):
        QWidget.__init__(self, None)
        self.x = x
        self.y = y
        self.sprite = sprite
        self.tile_id = tile_id
        self.current_frame = 0
        self.dirty = False

        self.connect(self.sprite, SIGNAL("painted()"), self.onpaint)

    def onpaint(self):
        self.dirty = True

    def advance_frame(self):
        if self.current_frame < (len(self.sprite.frames) - 1):
	        self.current_frame += 1
        else:
	        self.current_frame = 0

    def get_selected_frame(self):
        return self.sprite.frames[self.current_frame]

    def get_pixmap(self):
        pixmap = self.get_selected_frame().get_pixmap()
        return pixmap

    def todict(self):
        dict = {}
        for attr in MapTile.attrs:
            if attr == "frames":
                dict[attr] = len(self.sprite.frames)
            else:
                dict[attr] = getattr(self, attr, None)

        return dict
